﻿
CREATE PROCEDURE [dbo].[proc_CallCenter_EmpRecordlist]
	(
		@CompanyId int,
		@UserName nvarchar(50),
		@StartDate nvarchar(10),
		@EndDate nvarchar(10),
		@StartIndex int,
		@EndIndex int
	)
AS
Begin
	Declare @sRq Datetime,@eRq Datetime
	Set @sRq = Convert(Datetime,@StartDate+' 00:00:00')
	Set @eRq = Convert(Datetime,@EndDate+' 23:59:59')

	If Left(@UserName,1) = '-'
		Select Top 200 ROW_NUMBER() OVER (ORDER BY r.Id DESC)AS Row,
		Id,
		GId,
		ZuoxiID,
		[Type],
		Tel,
		IsNull(StartDate,'1900-01-01') AS StartDate,
		IsNull(EndDate,'1900-01-01') AS EndDate,
--		(Case When [Type]='CH_FREE' Then 0 Else IsNull(DateDiff(ss,StartDate,EndDate),0) End) As Seconds,
		0 As Seconds,
		TxtFileName,
		[FileName],
		CreateDate,
		CompanyId,
		DepartmentId,
		ZuoxiID As UserName,
		Convert(varchar(10),ZuoxiID)+'(坐席号)' As EName,
		IsNull((Select CompanyName From Company Where Id=@CompanyId),'---') As CompanyName,
		'无' As Department
		From CallCenter_Record r
		Where CompanyId=@CompanyId And r.ZuoxiID=-1*Convert(int,@UserName) And EndDate>=@sRq And EndDate<=@eRq
		Order By Row
	Else
		Select Top 200 ROW_NUMBER() OVER (ORDER BY r.Id DESC)AS Row,
		Id,
		GId,
		ZuoxiID,
		[Type],
		Tel,
		IsNull(StartDate,'1900-01-01') AS StartDate,
		IsNull(EndDate,'1900-01-01') AS EndDate,
		(Case When [Type]='CH_FREE' Then 0 Else IsNull(DateDiff(ss,StartDate,EndDate),0) End) As Seconds,
		TxtFileName,
		[FileName],
		CreateDate,
		CompanyId,
		DepartmentId,
		UserName,
		EName,
		ISNULL((Select CompanyName From Company Where Id=r.CompanyId),'---') As CompanyName,
		ISNULL((Select DName From Department Where Id=r.DepartmentId),'---') As Department
		From CallCenter_Record r
		Where CompanyId=@CompanyId And r.ZuoxiID=Convert(int,@UserName) And EndDate>=@sRq And EndDate<=@eRq
		Order By Row
End